//
//  BPLocationViewController.m
//  BP_NewClient
//
//  Created by letv on 15/7/19.
//  Copyright (c) 2015年 normal. All rights reserved.
//  定位功能-高德

#import "BPLocationViewController.h"
#import "ReGeocodeAnnotation.h"

@interface BPLocationViewController ()

@end

@implementation BPLocationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


// 定位
- (void)startLocation:(MALocationSuccessBlock)locationBlock errorBlock:(MALocationErrorBlock)locErrorBlock{

    [self initMapView];
    [self initSearch];

    self.locationBool = YES;
    self.mapView.showsUserLocation = YES;
    self.mapView.userTrackingMode = MAUserTrackingModeNone;

    self.myLocSuccessBlock = locationBlock;
    self.myLocErrorBlock = locErrorBlock;
}

#pragma -mark MAMapViewDelegate

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation
updatingLocation:(BOOL)updatingLocation
{
    if(updatingLocation && self.locationBool)
    {
        self.locationBool = NO;
        //取出当前位置的坐标
//        NSLog(@"latitude : %f,longitude: %f",userLocation.coordinate.latitude,userLocation.coordinate.longitude);
        /**
         *  1.定位坐标
         *  2.进行反地理编码
         */
        [self searchReGeocodeWithCoordinate:userLocation.coordinate];
    }
}

- (void)mapView:(MAMapView *)mapView didFailToLocateUserWithError:(NSError *)error{

    NSDictionary *dic = @{@"status":@"0",@"description":error.description};
    NSLog(@"dic is %@",dic);
    if (self.myLocErrorBlock) {
        self.myLocErrorBlock(dic);
    }
}


#pragma mark - AMapSearchDelegate

- (void)searchReGeocodeWithCoordinate:(CLLocationCoordinate2D)coordinate
{
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    
    regeo.location = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    regeo.requireExtension = YES;
    
    [self.search AMapReGoecodeSearch:regeo];
}

/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil)
    {
//        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
//        ReGeocodeAnnotation *reGeocodeAnnotation = [[ReGeocodeAnnotation alloc] initWithCoordinate:coordinate
//                                                                                         reGeocode:response.regeocode];
        
        if (self.myLocSuccessBlock) {
            self.myLocSuccessBlock(response.regeocode.addressComponent);
        }
/*
        [self.mapView addAnnotation:reGeocodeAnnotation];
        [self.mapView selectAnnotation:reGeocodeAnnotation animated:YES];
 */
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
